//
//  AudientListCell.m
//  AudientList
//
//  Created by zhangweiwei on 16/6/15.
//  Copyright © 2016年 Erica. All rights reserved.
//

#import "ZYAudientListCell.h"
#import "ZYTagView.h"
#import "Masonry.h"
#import "YYWebImage.h"
#import "YYCategories.h"
#import "KCCategory.h"
#import "ZYDiscountTagView.h"
#import "ZYAudioButton.h"

NSString *const ZYAudientListCellReuseID = @"ZYAudientListCell";

CGFloat  const ZYAudientListCellExceptIntroHeight = 183;

NSInteger  const ZYAudientListCellIntroFontSize = 14;

extern CGFloat const ZYTagCellHeight;

@interface ZYAudientListCell ()

@property (nonatomic, strong) UIView *container;

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIImageView *vView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *introLabel;
@property (nonatomic, strong) UIButton *priceView;
@property (nonatomic, strong) UIView *seperator;
@property (nonatomic, strong) ZYTagView *tagView;
@property (nonatomic, strong) UIButton *commentButton;
@property (nonatomic, strong) UILabel *soldTimeLabel;
@property (nonatomic, strong) ZYDiscountTagView *discountTagView;
@property (nonatomic, strong) ZYAudioButton *audioButton;

@end

@implementation ZYAudientListCell

#pragma mark -懒加载属性

- (ZYAudioButton *)audioButton
{
    if (!_audioButton) {
        _audioButton = [[ZYAudioButton alloc] init];
        [_audioButton addTarget:self action:@selector(audioButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _audioButton;
}

- (UIView *)container
{
    if (!_container) {
        _container = [UIView new];
        _container.backgroundColor = [UIColor whiteColor];
    }
    return _container;
}

- (UIButton *)commentButton
{
    if (!_commentButton) {
        _commentButton = [[UIButton alloc] init];
        [_commentButton setTitle:@"100%好评 >" forState:UIControlStateNormal];
        _commentButton.titleLabel.font = [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize];
        [_commentButton setTitleColor:[UIColor kc_colorWithHexValue:0xff6600] forState:UIControlStateNormal];
    }
    return _commentButton;
}

- (UIView *)seperator
{
    if (!_seperator) {
        _seperator = [UIView new];
        _seperator.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    }
    return _seperator;
}

- (UIImageView *)iconView
{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init];
    }
    return _iconView;
}

- (UIImageView *)vView
{
    if (!_vView) {
        _vView = [[UIImageView alloc] init];
    }
    return _vView;
}

- (UILabel *)soldTimeLabel
{
    if (!_soldTimeLabel) {
        _soldTimeLabel = [[UILabel alloc] init];
        _soldTimeLabel.font = [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize];
        _soldTimeLabel.textColor = [UIColor kc_colorWithHexValue:0x999999];
    }
    return _soldTimeLabel;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:17];
        _nameLabel.textColor = [UIColor darkGrayColor];
    }
    return _nameLabel;
}

- (UILabel *)introLabel
{
    if (!_introLabel) {
        _introLabel = [[UILabel alloc] init];
        _introLabel.font = [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize];
        _introLabel.textColor = [UIColor kc_colorWithHexValue:0x999999];
        _introLabel.numberOfLines = 0;
        _introLabel.userInteractionEnabled = YES;
        
        KC_WEAK_SELF
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            
            if (weakSelf.audientListModel.isSingleLineIntro) return;
            
            !weakSelf.AudienListCellDidClickIntro ? : weakSelf.AudienListCellDidClickIntro();
            
        }];
        [_introLabel addGestureRecognizer:tap];
    }
    return _introLabel;
}


- (ZYDiscountTagView *)discountTagView
{
    if (!_discountTagView) {
        _discountTagView = [[ZYDiscountTagView alloc] init];
    }
    return _discountTagView;
}

- (UIButton *)priceView
{
    if (!_priceView) {
        _priceView = [[UIButton alloc] init];
        [_priceView setBackgroundImage:[UIImage imageNamed:@"price_bg_male"] forState:UIControlStateNormal];
        [_priceView setBackgroundImage:[UIImage imageNamed:@"price_bg_female"] forState:UIControlStateSelected];
        [_priceView setTitleColor:[UIColor kc_colorWithHexValue:0x67d3ea] forState:UIControlStateNormal];
        [_priceView setTitleColor:KC_RGB_COLOR(251,117,135) forState:UIControlStateSelected];
        
        _priceView.titleLabel.font = [UIFont systemFontOfSize:14];
        _priceView.userInteractionEnabled = NO;
        
    }
    return _priceView;
}

- (ZYTagView *)tagView
{
    if (!_tagView) {
        _tagView = [[ZYTagView alloc] init];
    }
    return _tagView;
}

#pragma mark -初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI
{
    [self.contentView addSubview:self.container];
    [self.container addSubview:self.iconView];
    [self.container addSubview:self.nameLabel];
    [self.container addSubview:self.audioButton];
    [self.container addSubview:self.tagView];
    [self.container addSubview:self.priceView];
    [self.container addSubview:self.soldTimeLabel];
    [self.container addSubview:self.commentButton];
    [self.container addSubview:self.introLabel];
    [self.container addSubview:self.seperator];
    [self.container addSubview:self.vView];
    [self.container addSubview:self.discountTagView];
}

- (void)setupConstraints
{
    // 内层容器
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        
    }];
    
    // 头像
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.container).offset(10);
        make.top.equalTo(self.container).offset(27);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    
    // 音频按钮
    [self.audioButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).offset(10);
        make.width.equalTo(self.iconView);
        make.centerX.equalTo(self.iconView);
        make.height.equalTo(@25);
        
    }];
    
    // 头像右下角等级控件
    [self.vView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.iconView);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    // 昵称
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(20);
        make.top.equalTo(self.iconView);
        make.height.equalTo(@20);
    }];
    
    // 昵称右边折扣标签
    [self.discountTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        make.top.bottom.equalTo(self.nameLabel);
        make.right.equalTo(self.container).offset(-10);
    }];
    
    // 标签
    [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.container).offset(-10);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.height.equalTo(@(ZYTagCellHeight * 2 + 5));
    }];
    
    // 价格
    [self.priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.container);
        make.right.equalTo(self.container).offset(-20);
        make.size.mas_equalTo(CGSizeMake(90, 21));
    }];
    
    // 评价
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tagView);
        make.top.equalTo(self.tagView.mas_bottom).offset(10);
        make.height.equalTo(@20);
    }];
    
    // 出售时间
    [self.soldTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.commentButton);
        make.left.equalTo(self.commentButton.mas_right).offset(20);
        make.height.equalTo(self.commentButton);
    }];
    
    
    // 分割线
    [self.seperator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.commentButton.mas_bottom).offset(10);
        make.height.equalTo(@1);
        make.left.right.equalTo(self.container);
    }];
    
    // 简介
    [self.introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.container).offset(10);
        make.right.equalTo(self.container).offset(-10);
        make.bottom.equalTo(self.container).offset(-10);
        make.top.equalTo(self.seperator.mas_bottom).offset(10);
    }];
    
}


- (void)setAudientListModel:(ZYAudientListModel *)audientListModel
{
    _audientListModel = audientListModel;
    
    self.nameLabel.text = audientListModel.username;
    
    [self.iconView yy_setImageWithURL:[NSURL URLWithString:audientListModel.normalPic] placeholder:nil options:YYWebImageOptionProgressiveBlur|YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        
        self.iconView.image = [image kc_circleImage];
    }];
    
    /*** 这句代码内部比较消耗性能，需要优化 ***/
    self.tagView.tagModels = audientListModel.tagModels;
    /******/
    
    self.soldTimeLabel.text = [NSString stringWithFormat:@"已售%@小时", audientListModel.callTime];
    
    self.priceView.selected = audientListModel.sex == ZYSexTypeFemale;
    
    [self.priceView setTitle:[NSString stringWithFormat:@"￥%@起", audientListModel.miniCharge] forState:(audientListModel.sex == ZYSexTypeFemale ? UIControlStateSelected : UIControlStateNormal)];
    
    [self setupIntro];
    
    if (audientListModel.audioIntroSecond.length) {
        
        self.audioButton.hidden = NO;
        self.audioButton.sex = audientListModel.sex;
        self.audioButton.second = audientListModel.audioIntroSecond;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.audioButton.animating = audientListModel.isPlayAudio;
        });
        
        
    }else {
        self.audioButton.hidden = YES;
    }

    if (audientListModel.discountTagModels) {
        self.discountTagView.hidden = NO;
        self.discountTagView.tagModels = audientListModel.discountTagModels;
    }else {
        
        self.discountTagView.hidden = YES;
    }
    
    self.vView.image = [UIImage imageNamed:[NSString stringWithFormat:@"V%zd", audientListModel.isV]];
    

}

- (void)setupIntro
{
    
    if (self.audientListModel.isSingleLineIntro) {
        
        self.introLabel.attributedText = self.audientListModel.detailIntroAttributedString;
        
    }else {
    
        NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
        attrDict[NSFontAttributeName] = self.introLabel.font;
        attrDict[NSForegroundColorAttributeName] = [UIColor kc_colorWithHexValue:0x2abbb4];
        
        NSMutableAttributedString *attrStr = nil;
        if (self.audientListModel.isOpen) {
            
            attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.audientListModel.detailIntroAttributedString];
            [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"[收起]" attributes:attrDict]];
        }else {
            
            attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.audientListModel.shortIntroAttributedString];
            [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"[展开]" attributes:attrDict]];
            
        }
    
        self.introLabel.attributedText = attrStr;
        
    }
    
}

#pragma mark -Action

- (void)audioButtonClick
{
    self.audioButton.animating = !self.audioButton.isAnimating;
    
    self.audientListModel.playAudio = self.audioButton.isAnimating;
    
    !self.AudienListCellDidClickAudio ? : self.AudienListCellDidClickAudio();
}



@end
